package com.java2007.zhangyu.restaurant.controller;

import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodTypeService;
import com.java2007.zhangyu.restaurant.service.impl.FoodTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zy
 * @Version
 * @date 2021/01/23
 */
@WebServlet("/foodtype/save")
public class FoodTypeSaveServlet extends HttpServlet {

    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            //获取请求参数
            String fTypeName = request.getParameter("fTypeName");
            //根据菜品类别名增加菜品类别
            foodTypeService.saveByFoodTypeName(fTypeName);
            //重定向到查询接口
            response.sendRedirect(request.getContextPath() + "/foodtype/search");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙，请稍后重试！");
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
